# 运行鸿蒙一键构建部署脚本（多 HSP 包模式）- Windows PowerShell 版本
# 使用前请确保 hdc 已连接设备，DevEco Studio 工具链已安装

$ErrorActionPreference = "Stop"

# ================== 可配置变量 ==================
# 项目包名(根据实际项目包名修改!!!!!)
$BUNDLE_NAME = "heima.item.myapplication"
# ================== 可配置变量 ==================

# ================== 固定的变量 ==================
# 临时目录名（使用随机字符串避免冲突）
$RANDOM_STR = -join ((48..57) + (97..102) | Get-Random -Count 32 | ForEach-Object {[char]$_})
$TMP_DIR = "hm_deploy_tmp_$RANDOM_STR"

# HSP 和 HAP 包路径（根据构建输出调整为实际文件名）
$BASIC_HSP = "commons\basic\build\default\outputs\default\basic-default-unsigned.hsp"
$ENTRY_HAP = "product\phone\build\default\outputs\default\phone-default-unsigned.hap"
# 其他 HSP 模块
$HOME_HSP = "features\home\build\default\outputs\default\home-default-unsigned.hsp"
$SEARCH_HSP = "features\Search\build\default\outputs\default\Search-default-unsigned.hsp"
$MINE_HSP = "features\Mine\build\default\outputs\default\Mine-default-unsigned.hsp"
$COMMUNITY_HSP = "features\Community\build\default\outputs\default\community-default-unsigned.hsp"
# ================== 固定的变量 ==================

try {
    Write-Host "1. 安装依赖..." -ForegroundColor Cyan
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl true
    if ($LASTEXITCODE -ne 0) { throw "依赖安装失败" }

    Write-Host "`n2. 构建项目..." -ForegroundColor Cyan
    hvigorw assembleApp
    if ($LASTEXITCODE -ne 0) { throw "项目构建失败" }

    Write-Host "`n3. 停止正在运行的应用..." -ForegroundColor Cyan
    hdc shell aa force-stop $BUNDLE_NAME 2>$null
    # 忽略停止失败（应用可能未运行）

    Write-Host "`n4. 创建设备临时目录..." -ForegroundColor Cyan
    hdc shell mkdir -p data/local/tmp/$TMP_DIR
    if ($LASTEXITCODE -ne 0) { throw "创建设备临时目录失败" }

    Write-Host "`n5. 传输 HSP 和 HAP 包到设备..." -ForegroundColor Cyan
    
    # 传输基础模块 HSP
    Write-Host "  - 传输 basic.hsp..." -ForegroundColor Yellow
    hdc file send $BASIC_HSP "data/local/tmp/$TMP_DIR"
    if ($LASTEXITCODE -ne 0) { throw "传输 basic.hsp 失败" }

    # 传输 Home HSP
    Write-Host "  - 传输 home.hsp..." -ForegroundColor Yellow
    hdc file send $HOME_HSP "data/local/tmp/$TMP_DIR"
    if ($LASTEXITCODE -ne 0) { throw "传输 home.hsp 失败" }

    # 传输 Search HSP
    Write-Host "  - 传输 Search.hsp..." -ForegroundColor Yellow
    hdc file send $SEARCH_HSP "data/local/tmp/$TMP_DIR"
    if ($LASTEXITCODE -ne 0) { throw "传输 Search.hsp 失败" }

    # 传输 Mine HSP
    Write-Host "  - 传输 Mine.hsp..." -ForegroundColor Yellow
    hdc file send $MINE_HSP "data/local/tmp/$TMP_DIR"
    if ($LASTEXITCODE -ne 0) { throw "传输 Mine.hsp 失败" }

    # 传输 Community HSP
    Write-Host "  - 传输 community.hsp..." -ForegroundColor Yellow
    hdc file send $COMMUNITY_HSP "data/local/tmp/$TMP_DIR"
    if ($LASTEXITCODE -ne 0) { throw "传输 community.hsp 失败" }

    # 传输 entry HAP（主应用）
    Write-Host "  - 传输 phone.hap..." -ForegroundColor Yellow
    hdc file send $ENTRY_HAP "data/local/tmp/$TMP_DIR"
    if ($LASTEXITCODE -ne 0) { throw "传输 phone.hap 失败" }

    Write-Host "`n6. 安装应用包..." -ForegroundColor Cyan
    hdc shell bm install -p data/local/tmp/$TMP_DIR
    if ($LASTEXITCODE -ne 0) { throw "应用安装失败" }

    Write-Host "`n7. 清理设备临时文件..." -ForegroundColor Cyan
    hdc shell rm -rf data/local/tmp/$TMP_DIR
    if ($LASTEXITCODE -ne 0) { 
        Write-Warning "清理临时文件失败，但不影响应用运行"
    }

    Write-Host "`n8. 启动应用..." -ForegroundColor Cyan
    hdc shell aa start -a EntryAbility -b $BUNDLE_NAME -m phone
    if ($LASTEXITCODE -ne 0) { throw "应用启动失败" }

    Write-Host "`n✅ 构建部署完成！应用已成功启动" -ForegroundColor Green
    
} catch {
    Write-Host "`n❌ 错误: $_" -ForegroundColor Red
    Write-Host "`n部署失败，请检查错误信息" -ForegroundColor Red
    exit 1
}

# 暂停以查看输出
Read-Host "按 Enter 键退出"
